#
# Nintendo DS Makefile
#

.PHONY: arm7bin arm9bin package clean

ifeq ($(strip $(DEVKITPRO)),)
$(error "DEVKITPRO must be set in your environment.")
endif

ifeq ($(strip $(DEVKITARM)),)
$(error "DEVKITARM must be set in your environment.")
endif

#
# Stolen from ds_rules
#
CC      := arm-eabi-gcc
CXX     := arm-eabi-g++
AR      := arm-eabi-ar
OBJCOPY := arm-eabi-objcopy
LIBNDS  := $(DEVKITPRO)/libnds

#
# NDS target rules
#
STRIP  = /bin/true

#
# Override library paths.
#

EXTRA_INCLUDES = -Iarch/nds/ndsScreens/build \
                 -Iarch/nds/ndsLibfat/nds/include \
                 -I$(LIBNDS)/include

EXTRA_LIBS = -Larch/nds/ndsScreens/lib \
             -Larch/nds/ndsLibfat/nds/lib \
             -L$(LIBNDS)/lib \
             -lfat -lm -lnds9

ARCH_CFLAGS   += ${EXTRA_INCLUDES} -DARM9 -D__NDS__ -Iarch/nds
ARCH_CXXFLAGS += ${ARCH_CFLAGS}
ARCH_LDFLAGS  += ${EXTRA_LIBS} -specs=ds_arm9.specs

#
# Vile hack, remove me ASAP
#
arch/nds/%.o: arch/nds/%.c
	$(if ${V},,@echo "  CC      " $<)
	${CC} -MD ${core_cflags} ${core_flags} -Wno-unused-macros -c $< -o $@

arm9bin: mzx
	${OBJCOPY} -O binary ${mzxrun} ${mzxrun}.arm9

arm7bin:
	${MAKE} -C arch/nds TARGET=${mzxrun}

package: arm7bin arm9bin
	ndstool -c ${mzxrun}.nds -7 arch/nds/${mzxrun}.arm7 -9 ${mzxrun}.arm9 -b arch/nds/icon.bmp "MegaZeux ${VERSION}"

clean:
	@${MAKE} -C arch/nds TARGET=${mzxrun} clean
	@rm -f ${mzxrun}.nds arch/nds/${mzxrun}.arm7 ${mzxrun}.arm9 arch/nds/*.{d,o}

#
# We're only interested in our packaged binary; remove the ELF intermediaries
#
build := build/${SUBPLATFORM}/games/megazeux
build: package ${build}
	${CP} arch/nds/pad.config ${build}
	${CP} ${mzxrun}.nds ${build}
	${RM} ${build}/${mzxrun} ${build}/${mzxrun}.debug

include arch/zip.inc
